package com.tudouji.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.swing.text.html.FormSubmitEvent;

/**
 * 发送短信功能
 * 
 * @author Administrator
 *
 */
@Slf4j
@Data
@Component
@ConfigurationProperties(prefix = "sms")
public class MessageUitls {
	private Config config;

	@Data
	public static class Config {
		private  String AliSendMsgUrl;
		private  String AccessKeyId;
		private  String AccessKeySecret;
		private  String SignName;
		private  String TemplateCode_Verification;
		private  String TemplateCode_VeriIdentity;
	}

	public int sendMessage(String Mobile, String Content, String TemplateCode) {
		String code = "";
		int reslut = -1;
		try {
			DefaultProfile profile = DefaultProfile.getProfile("default", this.config.AccessKeyId,
					this.config.AccessKeySecret);
			IAcsClient client = new DefaultAcsClient(profile);

			CommonRequest request = new CommonRequest();
			request.setMethod(MethodType.POST);
			request.setDomain(this.config.AliSendMsgUrl);
			request.setVersion("2017-05-25");
			request.setAction("SendSms");
			request.putQueryParameter("RegionId", "default");
			request.putQueryParameter("PhoneNumbers", Mobile);
			request.putQueryParameter("SignName", this.config.SignName);
			request.putQueryParameter("TemplateCode", TemplateCode);
			request.putQueryParameter("TemplateParam", Content);
			CommonResponse response = client.getCommonResponse(request);
			JSONObject json = JSONObject.parseObject(response.getData());
			log.info(response.getData());
			code = json.getString("Code");
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (code.equals("OK")) {
			reslut = 1;// 发送成功
		}
		return reslut;
	}
	
 
	
	
	
	
	
	/**
	 * 
	* @Title: initCommonRequest
	* @Description: 初始化短信参数
	* @param @return 
	* @return CommonRequest
	* @author liukx
	* @throws
	 */
	public CommonRequest initCommonRequest(MethodType requestType, String sendMsgUrl,
										   String version, String action, String regionId, String phoneNumbers, String SignName,
										   String templateCode, String content){
		CommonRequest request = new CommonRequest();
		request.setMethod(requestType);
		request.setDomain(sendMsgUrl);
		request.setVersion(version);
		request.setAction(action);
		request.putQueryParameter("RegionId", regionId);
		request.putQueryParameter("PhoneNumbers", phoneNumbers);
		request.putQueryParameter("SignName",SignName);
		request.putQueryParameter("TemplateCode", templateCode);
		request.putQueryParameter("TemplateParam", content);
		return request;
	}
	
	
	/**
	 * 
	* @Title: initCommonRequest
	* @Description: 初始化短信参数
	* @param mobile 手机号码
	* @param content 发送内容
	* @templateCode 模板
	* @return CommonRequest
	* @author liukx
	* @throws
	 */
//	public CommonRequest initCommonRequest(String mobile,String content,String templateCode) throws Exception{
//		return initCommonRequest(MethodType.POST,SYSTEMCONFIG.getString("AliSendMsgUrl"),"2017-05-25"
//				,"SendSms","default",mobile,SYSTEMCONFIG.getString("SignName"),templateCode,content);
//	}
	
	
	
	
	/**
	 * 
	* @Title: initCommonRequest
	* @Description: 初始化短信参数
	* @param mobile 手机号码
	* @param content 发送内容
	* @templateCode 模板
	* @return CommonRequest
	* @author liukx
	* @throws
	 */
	public CommonRequest initCommonRequest(String mobile,String content,String templateCode,String SignName) throws Exception{
		return initCommonRequest(MethodType.POST,this.config.AliSendMsgUrl,"2017-05-25"
				,"SendSms","default",mobile,SignName,templateCode,content);
	}

	/**
	 * 
	* @Title: sendMessage
	* @Description: 发送短信
	* @param @param Mobile
	* @param @param Content
	* @param @param TemplateCode
	* @param @return 
	* @return int
	* @author liukx
	* @throws
	 */
	public  JSONObject  sendSMS(String mobile, String content,String templateCode,String signName) throws Exception{
		DefaultProfile profile = DefaultProfile.getProfile("default", this.config.AccessKeyId,this.config.AccessKeySecret);
		IAcsClient client = new DefaultAcsClient(profile);
		CommonRequest request = initCommonRequest(mobile,content,templateCode,signName);
		CommonResponse response = client.getCommonResponse(request);
		return JSONObject.parseObject(response.getData());
	}
	
	
	/**
	 * 
	* @Title: sendVerificationMessage
	* @Description: 发送验证码
	* @param @param mobile
	* @param @param verCode  必须是json字符串
	* @param @return 
	* @author liukx
	* @throws
	 */
	public JSONObject sendVerificationSMS(String mobile,String verCode) throws Exception{
		return sendSMS(mobile,verCode,this.config.TemplateCode_Verification,this.config.SignName);
	}
	
	/**
	* @Title: sendVerificationMessage
	* @Description: 发送通知信息
	* @param @param mobile
	* @param @param templateCode
	* @param @return 
	* @author liukx
	* @throws
	 */
	public String sendAdviceSMS(String mobile,String context,String smsModelID,String signName) throws Exception{
		return sendSMS(mobile,context,smsModelID,signName).toString();
	}
	
	
	
	public static void main(String [] args){
		MessageUitls msg = new MessageUitls();
		try {
			//JSONObject result = 	msg.sendWarnSMS("17601001609","告警信息");
			
			JSONObject json  = new JSONObject();
			json.put("code","7453");
			
			JSONObject result =  msg.sendVerificationSMS("17601001609",json.toString());
		} catch (Exception e) {
		
			e.printStackTrace();
		}
	}
	
	
	

}
